package com.naiterui.ehp.bs.doctor.controller;

import com.naiterui.ehp.bp.constants.SwaggerTag;
import com.naiterui.ehp.bp.support.exception.BusinessException;
import com.naiterui.ehp.bp.support.exception.PublicService;
import com.naiterui.ehp.bp.support.exception.ResponseVO;
import com.naiterui.ehp.bs.doctor.exception.ExceptionCodes;
import com.naiterui.ehp.bs.doctor.service.user.IDoctorAuthService;
import com.naiterui.ehp.bs.doctor.service.user.ILicenceService;
import com.naiterui.ehp.bs.doctor.vo.LicenseParam;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;

@RestController
@RequestMapping("ad/doctorLicence")
@Api(tags = {SwaggerTag.DOCTOR + SwaggerTag.AUTH_RECORD})
public class AdDoctorFileController {

    private static final Logger LOGGER = LoggerFactory.getLogger(AdDoctorFileController.class);

    private final ILicenceService licenceService;
    private final IDoctorAuthService doctorAuthService;

    @Autowired
    public AdDoctorFileController(ILicenceService licenceService, IDoctorAuthService doctorAuthService) {
        this.licenceService = licenceService;
        this.doctorAuthService = doctorAuthService;
    }

    @RequestMapping(value = "/upload")
    @ApiOperation(value = "APP端医生资质文件上传", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @ApiImplicitParams({})
    public ResponseVO<Void> uploads(Long doctorId, LicenseParam licenseParam) throws BusinessException {
        LOGGER.info("APP端医生资质文件上传licenseParam:{}", licenseParam);
        if (doctorId == null) {
            throw new BusinessException(ExceptionCodes.PARAM_ERROR);
        }
        // 基本参数校验
        licenseParam.validate(false);

        doctorAuthService.updateAuth(doctorId, licenseParam);

        return PublicService.returnResponseVO(ExceptionCodes.SUCCESS);
    }

    @ApiOperation(value = "APP端文件上传", httpMethod = "POST", produces = MediaType.APPLICATION_JSON_VALUE)
    @PostMapping("/upload/{type}")
    @ApiImplicitParams({
        @ApiImplicitParam(name = "type", value = "文件类型(1:个人照片,2:工作证,3:医生执业证,4:身份证,5:职称证书,6:医师资格证,7:签章图片)",
        dataType = "int", paramType = "path", required = true),})
    public ResponseVO<String> upload(@NotNull(message = "上传文件不能为null") MultipartFile file, Long doctorId,
        @PathVariable int type) throws IOException, BusinessException {
        LOGGER.info("APP端文件上传 doctor:{}, type:{}", doctorId, type);
        String url = licenceService.upload(doctorId, file, type);
        return PublicService.returnResponseVO(url);
    }
}
